/**
 * project_store.js
 * 项目Store
 */

// 导入第三方库
const { mobx } = require('../../../config/npm_config');
const { observable, action, computed, runInAction, decorate } = mobx;
const { dayjs } = require('../../../config/npm_config');

// 导入资源Store
const storeConfig = require('../../store_config');
const { resourcesStore, CACHE_GROUP } = storeConfig;

// 项目Store类定义
class ProjectStore {
  // 状态定义
  loading = false;
  projects = [];
  currentProject = null;
  filter = {
    status: 'all',
    keyword: ''
  };
  
  // 计算属性
  get filteredProjects() {
    return this.projects.filter(project => {
      // 状态筛选
      if (this.filter.status !== 'all' && project.status !== parseInt(this.filter.status)) {
        return false;
      }
      
      // 关键词筛选
      if (this.filter.keyword && !project.name.includes(this.filter.keyword)) {
        return false;
      }
      
      return true;
    });
  }
  
  get projectCount() {
    return this.projects.length;
  }
  
  // 加载项目列表
  loadProjects() {
    this.loading = true;
    
    return new Promise((resolve, reject) => {
      // 模拟API请求
      setTimeout(() => {
        const mockProjects = [
          {
            id: 'p001',
            name: '项目一',
            status: 1,
            startDate: '2023-01-01',
            endDate: '2023-12-31',
            description: '这是项目一的描述'
          },
          {
            id: 'p002',
            name: '项目二',
            status: 2,
            startDate: '2023-02-01',
            endDate: '2023-10-31',
            description: '这是项目二的描述'
          },
          {
            id: 'p003',
            name: '项目三',
            status: 0,
            startDate: '2023-03-01',
            endDate: '2023-09-30',
            description: '这是项目三的描述'
          }
        ];
        
        runInAction(() => {
          this.projects = mockProjects;
          this.loading = false;
        });
        
        // 缓存到resourcesStore
        mockProjects.forEach(project => {
          resourcesStore.cacheResource(CACHE_GROUP.PROJECT, project.id, project);
        });
        
        resolve(mockProjects);
      }, 1000);
    });
  }
  
  // 设置筛选条件
  setFilter(filter) {
    runInAction(() => {
      this.filter = {
        ...this.filter,
        ...filter
      };
    });
  }
  
  // 清除筛选条件
  clearFilter() {
    runInAction(() => {
      this.filter = {
        status: 'all',
        keyword: ''
      };
    });
  }
  
  // 加载项目详情
  loadProjectDetail(id) {
    this.loading = true;
    
    // 先尝试从缓存获取
    const cachedProject = resourcesStore.getResource(CACHE_GROUP.PROJECT, id);
    if (cachedProject) {
      runInAction(() => {
        this.currentProject = cachedProject;
        this.loading = false;
      });
      return Promise.resolve(cachedProject);
    }
    
    // 模拟API请求
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const projectDetail = {
          id: id,
          name: `项目${id}`,
          status: 1,
          startDate: '2023-01-01',
          endDate: '2023-12-31',
          description: `这是项目${id}的详细描述`,
          members: [
            { id: 'u001', name: '张三' },
            { id: 'u002', name: '李四' }
          ],
          tasks: [
            { id: 't001', name: '任务1', status: 1 },
            { id: 't002', name: '任务2', status: 0 }
          ]
        };
        
        runInAction(() => {
          this.currentProject = projectDetail;
          this.loading = false;
        });
        
        // 缓存到resourcesStore
        resourcesStore.cacheResource(CACHE_GROUP.PROJECT, id, projectDetail);
        
        resolve(projectDetail);
      }, 1000);
    });
  }
  
  // 清理资源
  reset() {
    runInAction(() => {
      this.projects = [];
      this.currentProject = null;
      this.loading = false;
      this.filter = {
        status: 'all',
        keyword: ''
      };
    });
  }
}

// 使用decorate替代ES6装饰器
decorate(ProjectStore, {
  // 状态
  loading: observable,
  projects: observable,
  currentProject: observable,
  filter: observable,
  
  // 计算属性
  filteredProjects: computed,
  projectCount: computed,
  
  // 行为
  loadProjects: action,
  setFilter: action,
  clearFilter: action,
  loadProjectDetail: action,
  reset: action
});

// 创建单例并导出
const projectStore = new ProjectStore();
module.exports = projectStore; 